<?php
class UsersController extends DomController {
	
	public function profile() {
		$this->auth();
		$user = $this->getUser();
		
		if( $_POST['changePassword'] ) {
			$user->password = array($_POST['password1'],$_POST['password2'],$_POST['oldPassword']);
			
			if( $user->save() ) {
				$okMessage = 'Parool muudetud';
			} else {
				$errorMessage = implode('<br>', $user->getValidationMessages());
			}
			
		}
		
		if( $_POST['changeProfile'] ) {
			
			$user->firstName = $_POST['firstName'];
			$user->lastName = $_POST['lastName'];
			
			if( $user->save() ) {
				$okMessage = 'Profiil muudetud';
			} else {
				$errorMessage = implode('<br>', $user->getValidationMessages());
			}
		}
		
		$view = new DomView('Users/profile.html');
		$view->user = $user;
		$view->okMessage = $okMessage;
		$view->errorMessage = $errorMessage;
		$this->view->contents = $view->fetch();
		
	}
		
	public function register() {
		
		if( $_POST['register'] ) {
			$user = new User();
			$user->username = $_POST['username'];
			$user->password = array($_POST['password1'],$_POST['password2']);
			$user->email = array($_POST['email1'],$_POST['email2']);
			$user->rulesAccepted = true;
			$user->firstName  = $_POST['firstName'];
			$user->lastName  = $_POST['lastName'];
			
			if( !DomCaptcha::validate($_POST['code']) ) {
				$errorMessage = 'Kontroll kood ei ole õige!';
				
			} else if( $user->save() ) {
				$okMessage = 'Registreerimine õnnestus. Teile saadeti registreerimise kinnitamiseks email aadressile '.$user->email.'!';
				
				$body = 'Tere [$username],
				
				Te olete registreeritud
				Teie parool on [$password].
				Et konto aktiveerida külstage palun järgmist linki <a href='.$this->getUrl('confirmRegistration').'[$code]>siin</a>
				';

				$user->sendConfirmationEmail('Registreerimine', $body, array('from@mail.ee','From'), $_POST['password'] );
				
				
			} else {
				$errorMessage = implode('<br>', $user->getValidationMessages());
			}
		}
		
		$view = new DomView('Users/register.html');
		$view->user = $user;
		$view->okMessage = $okMessage;
		$view->errorMessage = $errorMessage;
		$this->view->contents  = $view->fetch();
		
	}
	
	public function captcha() {
		$c = new DomCaptcha();
		$c->showImage();
	}
	
	public function confirmRegistration( $code ) {
		$action = User::confirmUserEmail( $code );
		
		if( $action instanceof User )
			$message = 'Kasutaja aktiveeritud';
		else if( $action === 1)
			$message = 'Kasutajat ei leitud!';
		else if( $action === 2)
			$message = 'Kasutaja on juba aktiveeritud!';
		
		$this->view->addPartitioner('contents','Users/emailConfirmed.html');
		$this->view->message = $message;
	}
	
	public function lostPassword() {
		if( $_REQUEST['sendNewPassword'] ) {
			$user = User::getLostPasswordUser( $_REQUEST['email'], $_REQUEST['username'] );
			if( $user ) {
				$message  = 'Teie uus parool on [$password]. Kui te olete sisseloginud saate parooli ära muuta! ';
				$user->sendLostPasswordEmail( 'Kaotatud parool', $message, array('from@mail.ee','From') );
				
				$okMessage  = 'Uus parool on saadetud teie emailile!';
			} else {
				$errorMessage = 'Kasutajat ei leitud!';
			}
		}
		
		$this->view->addPartitioner('contents', 'Users/lostPassword.html');
		$this->view->okMessage = $okMessage;
		$this->view->errorMessage = $errorMessage;
		
	}
	
	
	public function setup() {
		
		// User is logged in
		if( $user = User::getLoggedInUser() ) {
			
			if( $_GET['logout'] ) {
				$user->logout();
				$this->goTo( $this->getSiteUrl() );
			}
			
			$this->getSite()->setUser( $user );
			
			$view = new DomView('Users/loggedIn.html');
			$view->user = $user;
			$this->view->userArea = $view->fetch();
		
			
		
		// User is not logged in.
		} else {
			$view = new DomView('Users/login.html');
			
			if( $_REQUEST['username'] && $_REQUEST['password'] ) {
				$result = User::loginByUsername( $_REQUEST['username'], $_REQUEST['password'] );
				
				if( 0 === $result ) {
					$this->goTo( $_SERVER['HTTP_REFERER'] );
				} else if( 100 == $result ) {
					$errorMessage = 'Kasutajat ei leitud';
				} else if( 101 == $result) {
					$errorMessage = 'Parool ei ole õige!';
				} else if( 1 == $result) {
					$errorMessage = 'Kasutaja ei ole aktiveeritud';
				} else if( 2 == $result) {
					$errorMessage = 'Kasutaja on blokeeritud!';
				} else {
					$errorMessage = 'Tundmatu viga nr ' . $result . '!';
				}
				$view->errorMessage = $errorMessage;		
			}
		
			$this->view->userArea = $view->fetch();
		}
		
	}
	
}
?>